package cn.iocoder.yudao.module.product.controller.admin.stockinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.stockinfo.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.stockinfo.StockInfoDO;
import cn.iocoder.yudao.module.product.service.stockinfo.StockInfoService;

@Tag(name = "管理后台 - 库存信息")
@RestController
@RequestMapping("/product/stock-info")
@Validated
public class StockInfoController {

    @Resource
    private StockInfoService stockInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建库存信息")
    @PreAuthorize("@ss.hasPermission('product:stock-info:create')")
    public CommonResult<Long> createStockInfo(@Valid @RequestBody StockInfoSaveReqVO createReqVO) {
        return success(stockInfoService.createStockInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新库存信息")
    @PreAuthorize("@ss.hasPermission('product:stock-info:update')")
    public CommonResult<Boolean> updateStockInfo(@Valid @RequestBody StockInfoSaveReqVO updateReqVO) {
        stockInfoService.updateStockInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除库存信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:stock-info:delete')")
    public CommonResult<Boolean> deleteStockInfo(@RequestParam("id") Long id) {
        stockInfoService.deleteStockInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得库存信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:stock-info:query')")
    public CommonResult<StockInfoRespVO> getStockInfo(@RequestParam("id") Long id) {
        StockInfoDO stockInfo = stockInfoService.getStockInfo(id);
        return success(BeanUtils.toBean(stockInfo, StockInfoRespVO.class));
    }

//    @GetMapping("/page")
//    @Operation(summary = "获得库存信息分页")
//    @PreAuthorize("@ss.hasPermission('product:stock-info:query')")
//    public CommonResult<PageResult<StockInfoRespVO>> getStockInfoPage(@Valid StockInfoPageReqVO pageReqVO) {
//        PageResult<StockInfoDO> pageResult = stockInfoService.getStockInfoPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, StockInfoRespVO.class));
//    }

    @GetMapping("/page")
    @Operation(summary = "获得库存信息分页")
    @PreAuthorize("@ss.hasPermission('product:stock-info:query')")
    public CommonResult<PageResult<StockInfoSaveReqVO>> getStockInfoList(@Valid StockInfoPageReqVO respVO) {
        PageResult<StockInfoSaveReqVO>  list = stockInfoService.getStockInfoList(BeanUtils.toBean(respVO, StockInfoSaveReqVO.class));
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出库存信息 Excel")
    @PreAuthorize("@ss.hasPermission('product:stock-info:export')")
    @OperateLog(type = EXPORT)
    public void exportStockInfoExcel(@Valid StockInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StockInfoDO> list = stockInfoService.getStockInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "库存信息.xls", "数据", StockInfoRespVO.class,
                        BeanUtils.toBean(list, StockInfoRespVO.class));
    }

}